﻿using Consul;
using Ebusiness_Framework.DistributedTransaction;
using Framework.LogCenter;
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.Configuration;
using NLog.Web;
using Serilog.Events;
using Serilog;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Serilog.Filters;
using Serilog.Sinks.RabbitMQ;

namespace Ebusiness_Framework.LogCenter
{
    public static class LogcenterCenterWebApplicationBuilderExtentions
    {
        /// <summary>
        /// 日志中心
        /// </summary>
        /// <param name="builder"></param>
        /// <returns></returns>
        public static WebApplicationBuilder AddLogCenter(this WebApplicationBuilder builder)
        {
            var config = builder.Configuration;
            var conf = config["ASPNETCORE_ENVIRONMENT"];
            //3、加载nlog.config
            builder.WebHost.ConfigureAppConfiguration((context, builder) =>
            {
                if (conf == "Development")
                    NLogBuilder.ConfigureNLog($"nlog.config");
                else
                    NLogBuilder.ConfigureNLog($"nlog.{conf}.config");
            });

            //2、集成NLog
            builder.WebHost.UseNLog();
            return builder;
        }

        public static WebApplicationBuilder AddDistributedLog(this WebApplicationBuilder builder)
        {
            var configuration = builder.Configuration;
            DistributedLogOptions options = configuration.GetSection("DistributedLog").Get<DistributedLogOptions>();

            var loggerConfiguration = new LoggerConfiguration()
#if DEBUG
       .MinimumLevel.Debug()
#else
            .MinimumLevel.Information()
#endif
       .Enrich.FromLogContext()
       .WriteTo.Async(c => c.Console());
            foreach (var item in options.LogLevel)
            {
                loggerConfiguration = loggerConfiguration.MinimumLevel.Override(item, LogEventLevel.Information);
            }
            loggerConfiguration = loggerConfiguration.WriteTo.Async(a => a.Logger(lc => lc        //数据写入到rabbitmq
                .Filter.ByIncludingOnly(Matching.FromSource(options.LogLevel[0])) // 命名空间前缀匹配
                .WriteTo.RabbitMQ((clientCfg, sinkCfg) =>    //数据写入到 RabbitMQ   ,需要手动创建交换机队列
                {
                    //clientCfg.Hostnames.Add("20.255.99.182");
                    clientCfg.Hostnames = new List<string> { options.RabbitMQ.Hostname };
                    clientCfg.Port = options.RabbitMQ.Port;
                    clientCfg.Username = options.RabbitMQ.Username;
                    clientCfg.Password = options.RabbitMQ.Password;
                    clientCfg.Exchange = options.RabbitMQ.Exchange;
                    clientCfg.ExchangeType = options.RabbitMQ.ExchangeType;
                    clientCfg.RoutingKey = options.RabbitMQ.RoutingKey;
                    clientCfg.VHost = options.RabbitMQ.VHost;

                    // 自动创建交换机
                    clientCfg.AutoCreateExchange = true;
                    clientCfg.DeliveryMode = RabbitMQDeliveryMode.Durable;
                }))
            );

            Log.Logger = loggerConfiguration.CreateLogger();

            // 2、使用Serilog
            builder.Host.UseSerilog();

            return builder;
        }
    }
}
